package com.tiejun.ge.mvc.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiejun.ge.mvc.entity.bo.UserBO;
import com.tiejun.ge.mvc.entity.po.UserPO;
import com.tiejun.ge.mvc.mapper.UserMapper;
import com.tiejun.ge.mvc.repository.UserRepository;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @program: springboot
 * @description: UserRepository实现类
 * @author: getiejun
 * @create: 2025-07-31 00:06
 **/
@Repository
public class UserRepositoryImpl extends ServiceImpl<UserMapper, UserPO> implements UserRepository {

    @Resource
    private UserMapper userMapper;


    @Override
    public IPage<UserBO> page(int pageNum, int pageSize) {
        // 1. 正确设置分页参数（当前页、每页大小），指定泛型
        Page<UserPO> page = new Page<>(pageNum, pageSize);

        // 2. 执行分页查询（空条件可简化）
        IPage<UserPO> userPOPage = userMapper.selectPage(page, new LambdaQueryWrapper<>());

        // 3. 转换为BO并保留分页信息
        return userPOPage.convert(po -> {
            UserBO bo = new UserBO();
            BeanUtil.copyProperties(po, bo); // 显式转换，避免隐式风险
            return bo;
        });
    }

    @Override
    public void insert(UserBO userBO) {
        userMapper.insert(BeanUtil.copyProperties(userBO, UserPO.class));
    }

    @Override
    public UserBO detail(Long id) {
        UserPO userPO = userMapper.selectById(id);
        return BeanUtil.copyProperties(userPO, UserBO.class);
    }
}
